package org.ws.task.model.protocol.base;

import org.ws.task.netty.codec.CodecUtil;

import java.io.Serializable;

public abstract class BaseMessage<T> implements Serializable {

    private int id;

    private byte[] body;
    //
    private Class<T> bodyClass;

    public T getBodyObject(){
        if(body == null || bodyClass == null){
            return null;
        }
        return CodecUtil.decode(body,bodyClass);
    }

    public void setBodyObject(T body){
        this.body = CodecUtil.encode(body);
        this.bodyClass = (Class<T>) body.getClass();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

}
